/**
 * Created by LaoXu on 2017/12/21.
 * @description:注册页面业务逻辑
 */
require(['config'], function () {
    require(['jquery', 'xutil', 'xVerify'], function ($, xutil, xVerify) {
        // 获取DOM元素
        var $regMobile = $('#reg-mobile');
        var $regNickname = $('#reg-nickname');
        var $regPassword = $('#reg-password');
        var $regCheckpwd = $('#reg-checkpwd');
        var $regBtn = $('#btn-reg');


        var regPage = {
            init(){
                this.bindEvent();
            },
            bindEvent(){
                var that = this;

                $regBtn.on('click', function () {
                    var mobile = $regMobile.val(),
                        userName = $regNickname.val(),
                        password = $regPassword.val(),
                        againPwd = $regCheckpwd.val(),
                        checkMobile = that.checkMobile(mobile),
                        checkNickName = that.checkNickName(userName),
                        checkPwd = that.checkPwd(password, againPwd);


                    // 校验
                    if (!checkMobile.isSuc) {
                        alert(checkMobile.info);
                        return;
                    }
                    if (!checkNickName.isSuc) {
                        alert(checkNickName.info);
                        return;
                    }
                    if (!checkPwd.isSuc) {
                        alert(checkPwd.info);
                        return;
                    }

                    // 请求注册接口
                    $.post(xutil.baseUrl + '/api/user/regUserInfo', {mobile, userName, password}, function (data) {
                        var checkServerCode = that.checkServerCode(data);
                        if (checkServerCode.isSuc) {
                            // 注册成功到登录页面
                            window.location.href = './login.html';
                        } else {
                            alert(checkServerCode.info);
                        }
                    });
                });
            },
            // 校验手机号码
            checkMobile(mobile){
                var isSuc = false,
                    info = "";
                if (xVerify.isStrEmpty(mobile)) {
                    info = "请输入手机号码";
                } else if (!xVerify.checkPhone(mobile)) {
                    info = "手机号码格式不正确";
                } else {
                    info = "校验成功";
                    isSuc = true;
                }
                return {isSuc, info};
            },
            // 校验密码
            checkPwd(password, againPwd){
                var isSuc = false,
                    info = "";
                if (xVerify.isStrEmpty(password)) {
                    info = "请输入密码";
                } else if (xVerify.isStrEmpty(againPwd)) {
                    info = "请输入确认密码";
                } else if (!xVerify.checkPwd(password)) {
                    info = "请输入6~16位字母或数字";
                } else if (password != againPwd) {
                    info = "再次输入密码不一致";
                } else {
                    info = "校验成功";
                    isSuc = true;
                }
                return {isSuc, info};
            },
            checkNickName (nickName){
                var isSuc = false,
                    info = "";
                if (xVerify.isStrEmpty(nickName)) {
                    info = "请输入昵称";
                } else if (xVerify.checkSpecial(nickName)) {
                    info = "含非法字符请重新输入";
                } else {
                    info = "校验成功";
                    isSuc = true;
                }
                return {isSuc, info};
            },
            // 校验服务器端是否成功
            checkServerCode(data){
                var isSuc = false,
                    info = "",
                    code = data.code,
                    dataInfo = data.datas;
                if (code === 0) {
                    isSuc = true;
                    info = '注册成功';
                } else if (code === 1) {
                    info = '用户已存在';
                } else if (code === 3) {
                    info = '请输入您的用户名或密码';
                } else if (code === 4) {
                    info = '用户信息未找到';
                } else {
                    info = dataInfo;
                }
                return {isSuc, info};
            }
        };

        regPage.init();
    });
});
